import { JXDef } from "../../../conventions/JXCommon";
import { GCtrl } from "../../../Core/GCtrl";
import GParam from "../../../Core/GEvent/GParam";
import GViewBase from "../../../Core/GView/GViewBase";
import { AudioMgr } from "../../../Core/Manager/AudioMgr";
import { CMsg, DETAILSBTN, ITEM_DETAIL_FLAG, SUIT } from "../../Common/Define";
import { JXColor } from "../../Common/JXColor";
import { L } from "../../Common/Language";
import UICreate from "../../Common/UICreate";
import { Res } from "../../Common/UIResources";
import { JXLocales } from "../../Common/Zh";
import { REquireItem } from "../../Data/RunTime/RunRaw/RItemAttach";
import GameMgr from "../../Logic/GameMgr";
import JXItem from "../Common/JXItem";
const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/EquipDetailsCtrl')

export default class EquipDetailsCtrl extends GViewBase {

    private _data: REquireItem = null;

    /**装备名称 */
    protected _equipName: string = null;
    /**强化等级 */
    protected _sLv: number = null;
    /**跳转信息 */
    protected _jumpEquip: jumpEquip = null;
    /**英雄数据 */
    protected _IHeroData: IHeroData = null;
    /**装备名称 */
    protected _equipNameArr = [];
    /**5件套装备名称创建 */
    protected _equipArr = new Array(5);
    /**5件套集齐属性是否展示 */
    protected _isEquip = false;
    /**集齐5件套数量 */
    protected _FireEquip: number = 0;
    /**装备按钮是否可以使用 */
    protected _detailsbtn: number = DETAILSBTN.DETAILSBTN0;

    @property({ type: cc.Node, tooltip: "装备信息" }) equipInfo: cc.Node = null;
    @property({ type: cc.Node, tooltip: "图标" }) icon: cc.Node = null;
    @property({ type: cc.Label, tooltip: "装备名称" }) equipName: cc.Label = null;
    @property({ type: cc.Node, tooltip: "套装信息父节点" }) suitInfoParent: cc.Node = null;
    @property({ type: cc.Node, tooltip: "套装信息" }) suitInfo: cc.Node = null;
    @property({ type: cc.Node, tooltip: "强化信息父节点" }) strongInfoParent: cc.Node = null;
    @property({ type: cc.Node, tooltip: "强化信息" }) strongInfo: cc.Node = null;
    @property({ type: cc.Label, tooltip: "穿戴等级" }) firstLv: cc.Label = null;
    @property({ type: cc.Label, tooltip: "穿戴经验" }) firstExp: cc.Label = null;
    @property({ type: cc.Node, tooltip: "穿戴按钮" }) equipIn: cc.Node = null;
    onGStart(data: REquireItem, jumpEquip: jumpEquip, IHeroData: IHeroData, detailsbtn = DETAILSBTN.DETAILSBTN0) {
        this._detailsbtn = detailsbtn;
        if (this._detailsbtn != DETAILSBTN.DETAILSBTN0) {
            this.equipIn.getComponent(cc.Button).interactable = false;
            this.equipIn.active = false;
        }
        // console.log('data', data);
        this._data = data;
        this._jumpEquip = jumpEquip;
        this._IHeroData = IHeroData;
        this.initCarName();
        this.initEquipInfo();
        let bool = false;
        this.equipIn.children[0].children[0].getComponent(cc.Label).string = bool == this._jumpEquip.bool ? '装备' : '卸下';
    }
    protected initEquipInfo() {
        //强化等级 + 英雄名字 + 装备名字
        // let heroName;
        // heroName = GameMgr.heroData.getRaw<SHeroDataRaw>(this._IHeroData.tableId).name;
        let sLv = null;
        if (this._data.sLv > 0) {
            sLv = '  +' + this._data.sLv + '  ' + this._data.itemData.raw.name;
        } else {
            sLv = this._data.itemData.raw.name;
        }
        this.equipName.string = sLv;
        this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab) => {
            let comp = this.assetImpl.addGChild<JXItem>(prefab);
            comp.node.parent = this.icon;
            // comp.node.setPosition(0, 0);
            let value = null;
            let equipData = GameMgr.RBagData.getEquipDataById(this._data.insId);
            if (equipData) {
                value = equipData.itemData;
            } else {
                value = this._data.itemData;
            }

            comp.setView(value, ITEM_DETAIL_FLAG.NO_NUM_NODE | ITEM_DETAIL_FLAG.NO_SLV | ITEM_DETAIL_FLAG.ICON_SCALE);
        })

        //装备属性
        // var propts = this._data.propts;
        var propts = GameMgr.equipData.getRaw<SEquipDataRaw>(this._data.equipId).property;
        propts.forEach((value, index) => {
            if (value == 0) {
                return;
            }
            this.assetImpl.font(Res.font.fzcy, (font) => {
                let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                if (index > 2) {
                    numStr = ((value * 100).toFixed(2) + "%");
                }
                let str = L(JXLocales.propts["info" + index], numStr);
                let lab = UICreate.label(str);
                lab.fontSize = 20;
                lab.lineHeight = 20;
                lab.node.color = JXColor.Cffffff;
                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                // lab.node.anchorX = -0.3;
                lab.node.anchorX = 0;
                lab.spacingX = 5;
                lab.node.x = 25;
                this.equipInfo.addChild(lab.node);
                lab.node.addComponent(cc.LabelOutline);
                let line = lab.node.getComponent(cc.LabelOutline);
                line.color = cc.color(8, 89, 152, 255);
                line.width = 2;
                lab.font = font;
            })

        })

        //套装属性
        let suit = GameMgr.suitData.data.values();
        var heroArr = null;
        //该英雄目前所有可以使用的套装
        if (this._IHeroData) {
            heroArr = GameMgr.suitData.getAllHeroSuit(this._IHeroData.tableId);
        } else {
            let heroName = GameMgr.equipData.getRaw<SEquipDataRaw>(this._data.equipId).target[0];
            heroArr = GameMgr.suitData.getAllHeroSuit(heroName);
        }

        var suitArr = GameMgr.suitData.getHeroSuit(this._data.equipId, heroArr);
        let equipSuitArr: number[] = new Array<number>();//存储套装strID 判断是否重复穿戴了同装备id的装备兵书 导致套装集齐数量累加
        if (suitArr) {
            this.suitInfoParent.active = true;
            suitArr.suit.forEach((suit, index) => {
                if (this._equipArr[index] != true) {
                    let heroName = GameMgr.equipData.getRaw<SEquipDataRaw>(suit).name;
                    this.assetImpl.font(Res.font.fzcy, (font) => {
                        let lab = UICreate.label(heroName);
                        lab.fontSize = 20;
                        lab.lineHeight = 25;
                        lab.node.color = JXColor.Cffffff;
                        lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                        // lab.node.anchorX = -0.25;
                        lab.node.anchorX = 0;
                        lab.spacingX = 5;
                        lab.node.x = 25;
                        // console.log('该英雄目前所有可以使用的套装', lab.node.x);

                        this.suitInfo.addChild(lab.node);
                        lab.node.zIndex = index - index;

                        if (this._IHeroData) {
                            this._IHeroData.equips.forEach((equip, count) => {
                                if (equip != "-1" && equip) {//装备id有的情况下
                                    let equipData = GameMgr.RBagData.getEquipDataById(equip);
                                    if (suit == equipData.equipId) {
                                        if (count >= 3) {
                                            if (equipSuitArr.length > 0) {
                                                equipSuitArr.forEach((v) => {
                                                    if (v == equipData.equipId) {
                                                        return;
                                                    } else {
                                                        this._FireEquip++;
                                                        lab.node.color = cc.color(98, 247, 105, 255);
                                                        equipSuitArr.push(equipData.equipId);
                                                    }
                                                })
                                            } else {
                                                this._FireEquip++;
                                                lab.node.color = cc.color(98, 247, 105, 255);
                                                equipSuitArr.push(equipData.equipId);
                                            }
                                        } else {
                                            this._FireEquip++;
                                            lab.node.color = cc.color(98, 247, 105, 255);
                                        }
                                    }
                                }
                            })
                        }

                        lab.node.addComponent(cc.LabelOutline);
                        let line = lab.node.getComponent(cc.LabelOutline);
                        line.color = cc.color(8, 89, 152, 255);
                        line.width = 2;
                        lab.font = font;
                    })

                    if (suit == this._data.equipId) {
                        this._isEquip = true;
                    }
                    this._equipArr[index] = true;
                }

                if (this._isEquip && index == suitArr.suit.length - 1) {
                    this._isEquip = false;
                    this.assetImpl.font(Res.font.fzcy, (font) => {
                        let str = suitArr.name + '(' + this._FireEquip + '/' + suitArr.suit.length + ')';
                        let lab = UICreate.label(str);
                        lab.fontSize = 23;
                        lab.lineHeight = 30;
                        if (this._FireEquip > 0) {
                            lab.node.color = cc.color(98, 247, 105, 255);
                        } else {
                            lab.node.color = JXColor.Cffffff;
                        }

                        lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                        // lab.node.anchorX = -0.15;
                        lab.node.anchorX = 0;
                        lab.node.x = 25;
                        lab.spacingX = 5;
                        this.suitInfo.addChild(lab.node);
                        lab.node.zIndex = -10;
                        lab.node.addComponent(cc.LabelOutline);
                        let line = lab.node.getComponent(cc.LabelOutline);
                        line.color = cc.color(8, 89, 152, 255);
                        line.width = 2;
                        lab.font = font;
                    })


                    if (suitArr.up2) {
                        suitArr.up2.forEach((value, index) => {
                            if (value == 0) {
                                return;
                            }
                            this.assetImpl.font(Res.font.fzcy, (font) => {
                                let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                                let str = '(2)' + L(JXLocales.propts["info" + index], numStr);
                                let lab = UICreate.label(str);
                                lab.fontSize = 20;
                                lab.lineHeight = 25;
                                lab.spacingX = 5;
                                lab.node.color = JXColor.Cffffff;
                                if (this._FireEquip >= SUIT.SUIT2) {
                                    lab.node.color = cc.color(98, 247, 105, 255);
                                }
                                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                                // lab.node.anchorX = -0.2;
                                lab.node.anchorX = 0;
                                lab.node.x = 25;
                                this.suitInfo.addChild(lab.node);

                                lab.node.addComponent(cc.LabelOutline);
                                let line = lab.node.getComponent(cc.LabelOutline);
                                line.color = cc.color(8, 89, 152, 255);
                                line.width = 2;
                                lab.font = font;
                            })
                        })
                    }

                    if (suitArr.up3) {
                        suitArr.up3.forEach((value, index) => {
                            if (value == 0) {
                                return;
                            }
                            this.assetImpl.font(Res.font.fzcy, (font) => {
                                let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                                let str = '(3)' + L(JXLocales.propts["info" + index], numStr);
                                let lab = UICreate.label(str);
                                lab.fontSize = 20;
                                lab.lineHeight = 25;
                                lab.node.color = JXColor.Cffffff;
                                if (this._FireEquip >= SUIT.SUIT3) {
                                    lab.node.color = cc.color(98, 247, 105, 255);
                                }
                                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                                // lab.node.anchorX = -0.2;
                                lab.node.anchorX = 0;
                                lab.node.x = 25;
                                lab.spacingX = 5;
                                this.suitInfo.addChild(lab.node);

                                lab.node.addComponent(cc.LabelOutline);
                                let line = lab.node.getComponent(cc.LabelOutline);
                                line.color = cc.color(8, 89, 152, 255);
                                line.width = 2;
                                lab.font = font;
                            })
                        })
                    }

                    if (suitArr.up4) {
                        suitArr.up4.forEach((value, index) => {
                            if (value == 0) {
                                return;
                            }
                            this.assetImpl.font(Res.font.fzcy, (font) => {
                                let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                                let str = '(4)' + L(JXLocales.propts["info" + index], numStr);
                                let lab = UICreate.label(str);
                                lab.fontSize = 20;
                                lab.lineHeight = 25;
                                lab.node.color = JXColor.Cffffff;
                                if (this._FireEquip >= SUIT.SUIT4) {
                                    lab.node.color = cc.color(98, 247, 105, 255);
                                }
                                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                                // lab.node.anchorX = -0.2;
                                lab.node.anchorX = 0;
                                lab.spacingX = 5;
                                lab.node.x = 25;
                                this.suitInfo.addChild(lab.node);

                                lab.node.addComponent(cc.LabelOutline);
                                let line = lab.node.getComponent(cc.LabelOutline);
                                line.color = cc.color(8, 89, 152, 255);
                                line.width = 2;
                                lab.font = font;
                            })

                        })
                    }

                    if (suitArr.up5) {
                        suitArr.up5.forEach((value, index) => {
                            if (value == 0) {
                                return;
                            }
                            this.assetImpl.font(Res.font.fzcy, (font) => {
                                let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                                let str = '(5)' + L(JXLocales.propts["info" + index], numStr);
                                let lab = UICreate.label(str);
                                lab.fontSize = 20;
                                lab.lineHeight = 25;
                                lab.node.color = JXColor.Cffffff;
                                if (this._FireEquip >= SUIT.SUIT5) {
                                    lab.node.color = cc.color(98, 247, 105, 255);
                                }
                                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                                // lab.node.anchorX = -0.2;
                                lab.node.anchorX = 0;
                                lab.spacingX = 5;
                                lab.node.x = 25;
                                this.suitInfo.addChild(lab.node);

                                lab.node.addComponent(cc.LabelOutline);
                                let line = lab.node.getComponent(cc.LabelOutline);
                                line.color = cc.color(8, 89, 152, 255);
                                line.width = 2;
                                lab.font = font;
                            })

                        })
                    }

                    if (suitArr.allUp) {
                        this.assetImpl.font(Res.font.fzcy, (font) => {
                            let lab = UICreate.label(suitArr.txt);
                            lab.fontSize = 20;
                            lab.lineHeight = 25;
                            lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                            // lab.node.anchorX = -0.12;
                            lab.node.anchorX = 0;
                            lab.spacingX = 5;
                            lab.node.x = 25;
                            lab.node.color = JXColor.Cffffff;
                            if (this._FireEquip >= SUIT.SUIT5) {
                                lab.node.color = cc.color(98, 247, 105, 255);
                            }
                            this.suitInfo.addChild(lab.node);
                            lab.node.addComponent(cc.LabelOutline);
                            let line = lab.node.getComponent(cc.LabelOutline);
                            line.color = cc.color(8, 89, 152, 255);
                            line.width = 2;
                            lab.font = font;
                        })

                    }
                }
            })
        } else {
            this.suitInfoParent.active = false;
        }
        if (this._data.sLv > 0) {
            this.strongInfoParent.active = true;


            this.assetImpl.font(Res.font.fzcy, (font) => {
                let str = null;
                str = '+' + (this._data.sLv) + '强化';
                let lab = UICreate.label(str);
                lab.fontSize = 23;
                lab.lineHeight = 30;
                lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                // lab.node.anchorX = -0.3;
                lab.node.anchorX = 0;
                lab.spacingX = 5;
                lab.node.x = 25;
                lab.node.color = cc.color(255, 180, 34, 255);
                this.strongInfo.addChild(lab.node);

                lab.node.addComponent(cc.LabelOutline);
                let line = lab.node.getComponent(cc.LabelOutline);
                line.color = JXColor.C000000;
                line.width = 2;
                lab.font = font;
            })

            let proptsEquip = this._data.propts;
            proptsEquip.forEach((value, index) => {
                value -= propts[index];
                if (value == 0) {
                    return;
                }
                this.assetImpl.font(Res.font.fzcy, (font) => {
                    let numStr = value < 1 ? (value * 100 + "%") : value.toString();
                    let str = L(JXLocales.propts["info" + index], '+' + numStr);
                    let lab = UICreate.label(str);
                    lab.fontSize = 20;
                    lab.lineHeight = 25;
                    lab.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
                    // lab.node.anchorX = -0.22;
                    lab.node.anchorX = 0;
                    lab.spacingX = 5;
                    lab.node.x = 25;
                    lab.node.color = cc.color(255, 180, 34, 255);
                    this.strongInfo.addChild(lab.node);

                    lab.node.addComponent(cc.LabelOutline);
                    let line = lab.node.getComponent(cc.LabelOutline);
                    line.color = JXColor.C000000;
                    line.width = 2;
                    lab.font = font;
                })
            })
        } else {
            this.strongInfoParent.active = false;
        }
        //首次穿戴信息以及等级
        this.assetImpl.font(Res.font.fzcy, (font) => {
            this.firstExp.string = '首次穿戴获得经验值:' + ' ' + this._data.itemData.raw.exp + '';

            if (this._data.isFirstExp) {
                this.firstExp.node.color = JXColor.C949494;
            } else {
                this.firstExp.node.color = cc.color(98, 247, 105, 255);
            }
            this.firstExp.node.anchorX = 0;
            this.firstExp.node.addComponent(cc.LabelOutline);
            let line = this.firstExp.node.getComponent(cc.LabelOutline);
            line.color = JXColor.C000000;
            line.width = 2;
            this.firstExp.font = font;
        })

        this.assetImpl.font(Res.font.fzcy, (font) => {
            if (this._data.itemData.raw.lv == 0) {
                this._data.itemData.raw.lv += 1;
            }
            let lvData: SLvDataRaw = GameMgr.lvData.getIdByInfo(this._data.itemData.raw.lv);
            // this.firstLv.string = '需要等级:' + '' + this._equipNameArr[this._data.quality - 1] + this._data.itemData.raw.lv + '';
            this.firstLv.string = '需要等级:' + '' + this._equipNameArr[lvData.quality - 1] + lvData.showLv + '级';
            this.firstLv.node.anchorX = 0;
            this.firstLv.node.addComponent(cc.LabelOutline);
            let line1 = this.firstLv.node.getComponent(cc.LabelOutline);
            line1.color = JXColor.C000000;
            line1.width = 2;
            this.firstLv.font = font;
        })

    }

    /**穿戴装备 */
    protected wearEquip() {
        let site = GameMgr.RBagData.getEquipDataById(this._data.insId).itemData.raw.seat;
        let itemLv = GameMgr.RBagData.getEquipDataById(this._data.insId).itemData.raw.lv;
        if (!this._jumpEquip.bool) {
            if (this._IHeroData.lv >= itemLv) {
                if (site > 2) {
                    if (this._IHeroData.equips.length > site) {
                        if (this._IHeroData.equips.length == 6) {
                            let bool = false;
                            this._IHeroData.equips.forEach((value, index) => {
                                if (!bool) {
                                    if (index == this._IHeroData.equips.length - 1) {
                                        site = index;
                                        bool = true;
                                    }
                                    if ((value == null || value == '-1') && index > 2) {
                                        site = index;
                                        bool = true;
                                    }
                                }
                            })
                        } else {
                            site = this._IHeroData.equips.length;
                        }
                    }
                }
                let EquipPutInfo: EquipPutInfo = {
                    heroId: this._IHeroData.tableId,
                    site: site,
                    equipId: this._data.insId
                }
                // console.log('穿戴装备', site, this._data.isFirstExp);
                if (this._data.isFirstExp != true) {
                    let info: HeroAddExp = {
                        heroId: this._IHeroData.tableId,
                        exp: this._data.itemData.raw.exp
                    }
                    GameMgr.RBagData.setIsFirstExp(this._data.insId);
                    GCtrl.ES.emit(CMsg.client.hero.onHeroAddExp, new GParam(info))
                }
                var heroArr = GameMgr.suitData.getAllHeroSuit(this._IHeroData.tableId);
                var suitArr: SSuitDataRaw = GameMgr.suitData.getHeroSuit(this._data.equipId, heroArr);

                let suitInfo: suitInfo = {
                    IHeroData: this._IHeroData,
                    suit: suitArr
                }
                AudioMgr.Ins().playEffect(Res.audio.addEquip);
                GCtrl.ES.emit(CMsg.client.hero.onAddSuit, new GParam(suitInfo));
                GCtrl.ES.emit(CMsg.client.equip.onEquipPutOn, new GParam(EquipPutInfo));
            } else {

                let lvData: SLvDataRaw = GameMgr.lvData.getIdByInfo(this._data.itemData.raw.lv);

                let str = '英雄等级不足，需要英雄达到' + this._equipNameArr[lvData.quality - 1] + lvData.showLv + '级';
                GameMgr.uiMgr.showToast(str);//JXLocales.tip.lvNO
            }
        } else {
            this._IHeroData.equips.forEach((value, index) => {
                if (this._data.insId == value) {
                    site = index;
                }
            })
            let EquipPutInfo: EquipPutInfo = {
                heroId: this._IHeroData.tableId,
                site: site,
                equipId: this._data.insId
            }
            // console.log('卸下装备', site);
            GCtrl.ES.emit(CMsg.client.equip.onEquipPutOff, new GParam(EquipPutInfo));
        }
        this.onClose();
    }


    /**初始化卡牌品质名称 */
    protected initCarName() {
        let white = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.white);
        this._equipNameArr.push(white);
        let green = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.green);
        this._equipNameArr.push(green);
        let Blue = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.Blue);
        this._equipNameArr.push(Blue);
        let purple = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.purple);
        this._equipNameArr.push(purple);
        let orange = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.orange);
        this._equipNameArr.push(orange);
        let red = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.red);
        this._equipNameArr.push(red);
    }

    public close() {
        this.onClose();
    }
}       